//
//  changePhoneViewController.m
//  dongfengTest
//
//  Created by wxt on 2021/3/17.
//

#import "ChangeWordViewController.h"
#import "ChangeSuccessViewController.h"
#import "ForgetPayWordViewController.h"
#import "ChangePassWordViewController.h"
#import "LognViewController.h"
@interface ChangeWordViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)TKLtextFiled * PhNumberF;
@property(nonatomic,strong)UIButton * isHiddenBtn;
@property(nonatomic,strong)UIButton * getVerBtn;
@property(nonatomic,strong)UIButton * pushUpBtn;
@property(nonatomic,strong)NSString *authorizationCode;
@property(nonatomic,strong)NSString *authorizationKey;

@end

@implementation ChangeWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

self.view.backgroundColor = Wcolor;

    UIButton * backBtn =[UIButton createBtnFrame:CGRectMake(15, kStatusBarHeight+5, 18, 18) title:@"" bgImageName:@"fanhui" action:^(UIButton *button) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [self.view addSubview:backBtn];
    TKLlable * navTit = [TKLlable labelWithFrame:CGRectMake(kWidth/2-60, kStatusBarHeight+5, 120, 20) text:@"" textColor:Bcolor font:Font_6(16) textAlignment:Textcenter backgroundColor:Ccolor];
    [self.view addSubview:navTit];

        navTit.text= @"修改登录密码";

      [self addUIView];
    
  
// Do any additional setup after loading the view.
}



-(void)addUIView{
 
  
    
    TKLlable * phoneNumLab = [TKLlable labelWithFrame:CGRectMake(20, kTopHeight+20, kWidth-40, 20) text:@"请输入当前登录密码" textColor:Bcolor font:Font_6(18) textAlignment:Textleft backgroundColor:Ccolor];
    [self.view addSubview:phoneNumLab];
    
    self.PhNumberF = [TKLtextFiled FiledWithFrame:CGRectMake(20, CGRectGetMaxY(phoneNumLab.frame)+50, kWidth-120, 40) text:@"  请输入登录密码" textColor:Bcolor font:Font_6(14) textAlignment:Textleft backgroundColor:Ccolor ];
    self.PhNumberF.secureTextEntry = YES;
    [self.PhNumberF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    
    self.isHiddenBtn = [UIButton createBtnFrame:CGRectMake(CGRectGetMaxX(self.PhNumberF.frame)+30, CGRectGetMaxY(phoneNumLab.frame)+60, 30, 20) title:@"" bgImageName:@"yincang" action:^(UIButton *button) {
        [self.view endEditing:YES];
        [self ishiddenAction];
        
    }];

    self.isHiddenBtn.selected = NO;
    [self.isHiddenBtn setTitleColor:Bcolor forState:UIControlStateNormal];
    [self.view addSubview:self.isHiddenBtn];
    
    
    
    [self.view addSubview:self.PhNumberF];
    UIView * linView = [[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.PhNumberF.frame)+5, kWidth-40, 1)];
    linView.backgroundColor = JhColor(204, 204, 204);
    [self.view addSubview:linView];
    
  
    
    
    self.pushUpBtn = [UIButton createBtnFrame:CGRectMake(20, CGRectGetMaxY(linView.frame)+20, kWidth-40, 40) title:@"下一步" bgImageName:@"" action:^(UIButton *button) {
        
        [self.view endEditing:YES];
        if ([TKLTools isPassWord:self.PhNumberF.text]) {
            [self userLoginUrl];
        }else{
            [MBProgressHUD showError:@"请输入正确的密码" toView:self.view];
        }
        
      

      
    }];
    self.pushUpBtn.backgroundColor = JhColor(75, 149, 251);
    self.pushUpBtn.layer.masksToBounds = YES;
    self.pushUpBtn.layer.cornerRadius = 20;
    [self.view addSubview:self.pushUpBtn];
    
    UIButton * forgetBtn = [UIButton createBtnFrame:CGRectMake(kWidth/2-50, CGRectGetMaxY(self.pushUpBtn.frame)+10, 100, 20) title:@"忘记登录密码>" bgImageName:@"" action:^(UIButton *button) {
        
        ForgetPayWordViewController * forgetVC = [[ForgetPayWordViewController alloc]init];
        forgetVC.pushPath = @"passWord";
        forgetVC.modalPresentationStyle = 0;
        
        [self presentViewController:forgetVC animated:YES completion:nil];
     
        
        
    }];
    [self.view addSubview:forgetBtn];
    [forgetBtn setTitleColor:JhColor(204, 204, 204) forState:UIControlStateNormal];
 
    
}
//监听长度
- (void)textFieldDidChange:(UITextField *)textField{
   
    if (textField == self.PhNumberF) {
        if (textField.text.length > 16) {
            self.PhNumberF.text = [textField.text substringToIndex:16];
        }
    }

}

-(void)userLoginUrl{
    

    NSUserDefaults * stand = [NSUserDefaults standardUserDefaults];
    
    NSDictionary * dic = @{@"pwd":self.PhNumberF.text,@"uno":[stand objectForKey:@"userName"]};


    [AFNetworkUtils POST_RawToken:kUser_VerifyUserPassword_Api body:dic.yy_modelToJSONString head:nil success:^(NSDictionary * _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
    
        if ([responseObject[@"code"] intValue] == 200) {
            

            ChangePassWordViewController * changVC = [[ChangePassWordViewController alloc]init];
            changVC.modalPresentationStyle = 0;
            
            [self presentViewController:changVC animated:YES completion:nil];
            
        }else if ([responseObject[@"code"] intValue] == 999) {
            
            [MBProgressHUD showMessage:@"请输入正确的密码" toView:self.view];
        }else if ([responseObject[@"code"] intValue] == 401) {
            
            //token过期
            [[NSNotificationCenter defaultCenter] postNotificationName:@"tokenNotime" object:@""];
            LognViewController * viewVC = [[LognViewController alloc]init];
            
            [UIApplication sharedApplication].keyWindow.rootViewController = viewVC;
          
        }else{
            [MBProgressHUD showMessage:responseObject[@"msg"] toView:self.view];
            
        }
        } failure:^(NSString * _Nonnull error) {
            NSLog(@"%@",error);
        }];

}
-(void)ishiddenAction{
    // 取反

    if (self.isHiddenBtn.selected == NO) {
        self.isHiddenBtn.selected = YES;
        [self.isHiddenBtn setBackgroundImage:[UIImage imageNamed:@"xianshi"] forState:UIControlStateNormal];
    }else{
        self.isHiddenBtn.selected = NO;
        [self.isHiddenBtn setBackgroundImage:[UIImage imageNamed:@"yincang"] forState:UIControlStateNormal];
    }
    
    self.PhNumberF.secureTextEntry = !self.PhNumberF.secureTextEntry;
 
// 获得输入的值

    NSString *content = self.PhNumberF.text;
  
 // 解决输入框从密文切换明文时出现空格的问题

    self.PhNumberF.text = @"";
 

    self.PhNumberF.text = content;

 //如果是密文转明文进行二次编辑不会出现清空现象,如果是明文转密文进行二次编辑就会出现清空现象,进行判断,如果切换后此时输入框是密文状态,就保存原有内容并赋值给输入框,这样就不会出现清空现象了

   if (self.PhNumberF.secureTextEntry) {

 // 解决输入框从明文切换为密文时进行二次编辑出现清空现象

       [self.PhNumberF insertText:content];
   

 }

 
    
    
}
-(void)dealloc{
  
    
        
        [[NSNotificationCenter defaultCenter] removeObserver:self];
 
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
